//
//  MineViewController.m
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/19.
//

#import "MineViewController.h"
#import "Masonry.h"
#import "DBStorageService.h"

@interface MineViewController ()

@property(nonatomic, strong) NSArray *datas;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    [self setupTableHeaderView];
}

- (void)setupTableView {
    CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - tabBarHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 58, 0, 0);
    [self.view addSubview:self.tableView];
    
    self.datas = @[
        @[
            @{@"title": @"服务", @"image": @"icon_payment"}
        ],
        @[
            @{@"title": @"收藏", @"image": @"icon_collection "},
            @{@"title": @"朋友圈", @"image": @"icon_moments"},
            @{@"title": @"视频号", @"image": @"icon_videos"},
            @{@"title": @"卡包", @"image": @"icon_cards"},
            @{@"title": @"表情", @"image": @"icon_stickers"}
        ],
        @[
            @{@"title": @"设置", @"image": @"icon_settings"}
        ]
    ];
    
    // 注册单元格
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MineCell"];
}

- (void)setupTableHeaderView {
    // 创建HeaderView
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    self.headerView.backgroundColor = [UIColor whiteColor];
    
    DBStorageService *storage = [DBStorageService sharedInstance];
    UserModel *user = [storage fetchUserById:@"1001"];
    
    // 添加头像
    self.avatarImageView = [[UIImageView alloc] init];
    self.avatarImageView.image = [UIImage imageNamed:user.avatar];
    self.avatarImageView.layer.cornerRadius = 35;
    self.avatarImageView.layer.masksToBounds = YES;
    [self.headerView addSubview:self.avatarImageView];
    
    // 添加昵称
    self.nicknameLabel = [[UILabel alloc] init];
    self.nicknameLabel.text = user.nickname;
    self.nicknameLabel.font = [UIFont systemFontOfSize:22];
    [self.headerView addSubview:self.nicknameLabel];
    
    // 添加微信号
    self.uidLabel = [[UILabel alloc] init];
    self.uidLabel.text = [NSString stringWithFormat:@"微信号: %@", user.id];
    self.uidLabel.font = [UIFont systemFontOfSize:16];
    self.uidLabel.textColor = [UIColor grayColor];
    [self.headerView addSubview:self.uidLabel];
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_top).offset(10);
        make.left.mas_equalTo(self.headerView.mas_left).offset(10);
        make.width.height.mas_equalTo(70);
    }];
    
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_top).offset(20);
        make.left.mas_equalTo(self.avatarImageView.mas_right).offset(15);
    }];
    
    [self.uidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nicknameLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.avatarImageView.mas_right).offset(15);
    }];
    
    
    // 设置为表格的 HeaderView
    self.tableView.tableHeaderView = self.headerView;
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.datas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineCell" forIndexPath:indexPath];
    // 1.获取数据
    NSDictionary *item = self.datas[indexPath.section][indexPath.row];
    NSString *title = item[@"title"];
    NSString *image = item[@"image"];
    
    // 2.配置单元格
    cell.textLabel.text = title;
    cell.imageView.image = [UIImage imageNamed:image];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // 3.调整分隔线
    if (indexPath.row == [self.datas[indexPath.section] count] - 1) {
        // 最后一行的分隔线从左边缘开始
        cell.separatorInset = UIEdgeInsetsZero;
    } else {
        // 其他行的分隔线从图标后开始
        cell.separatorInset = UIEdgeInsetsMake(0, 58, 0, 0);
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

// 设置 Footer 高度 控制组间间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

// 返回空的footer视图（避免默认样式）
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
